﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using EnergyConsumptionMonitorSystem.Components;

namespace EnergyConsumptionMonitorSystem.MainWindowTabItem
{
    public class MainWindowTabItemViewModel : INotifyPropertyChanged
    {
        private string _headerText;
        public string HeaderText
        {
            get { return this._headerText; }
            set
            {
                if (this._headerText != value)
                {
                    this._headerText = value;
                    NotifyPropertyChanged("HeaderText");
                }
            }
        }
        
        public string ItemID { get; set; }

        private TabItemContentUCViewModel _contentVM;
        public TabItemContentUCViewModel ContentVM
        {
            get { return this._contentVM; }
            set
            {
                if (this._contentVM != value)
                {
                    if (this._contentVM != null)
                    {
                        this._contentVM.CloseRequestEvent -= this.content_CloseRequestEvent;
                        this._contentVM.ContainerVM = null;
                    }
                    this._contentVM = value;
                    if (value != null)
                    {
                        this._contentVM.CloseRequestEvent += new EventHandler(content_CloseRequestEvent);
                        this._contentVM.ContainerVM = this;
                    }
                    NotifyPropertyChanged("Content");
                }
            }
        }

        public UserControl Content { get { return this._contentVM.View; } }

        private MainWindowTabItem _view;
        public MainWindowTabItem View { get { return this._view; } }

        public delegate void CloseRequestHandler(string itemID);
        public event CloseRequestHandler CloseRequestEvent;

        public event CancelEventHandler ClosingEvent;

        private ICommand _closeRequestCommand;
        public ICommand CloseRequestCommand { get { return this._closeRequestCommand; } }

        public MainWindowTabItemViewModel()
        {
            this._view = new MainWindowTabItem();
            this._view.DataContext = this;

            this._closeRequestCommand = new Command(new Action<object>(this.CloseRequest));
        }

        private void content_CloseRequestEvent(object sender, EventArgs e)
        {
            CloseRequest(null);
        }

        private void CloseRequest(object o)
        {
            if (this.ClosingEvent != null)
            {
                CancelEventArgs e = new CancelEventArgs();
                e.Cancel = false;
                this.ClosingEvent(this, e);
                if (!e.Cancel)
                {
                    if (this.CloseRequestEvent != null)
                    {
                        this.CloseRequestEvent(this.ItemID);
                    }
                }
            }
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
